<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts';

require('echarts/theme/macarons'); // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    nowTask: {
      type: Object,
    },
    height: {
      type: String,
      default: '250px',
    },
    chartData: {
      type: Array,
      required: true,
    },
    percent: {
      type: String,
      required: 0,
      default:0
    },
  },
  //   computed:{
  //   theme(){
  //     // this.themes=this.$store.state.board.thisTheme
  //     //  this.initChart(this.chartData)
  //     return this.$store.state.board.thisTheme
  //   },
  // },
  data() {
    return {
      chart: null,
      themes: 0,
    };
  },
  mounted() {
    this.$nextTick(() => {
      setTimeout(() => {
        this.initChart();
      }, 80);
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
    themes: {
      deep: true,
      handler(val) {
        this.themes = this.$store.state.board.thisTheme;
        this.setOptions(this.chartData);
      },
    },
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons');
      this.setOptions(this.chartData);
    },
    setOptions(chartData) {
      let percent = 0;
      if (isNaN(this.percent)) {
        percent = 0;
      } else {
        percent = this.percent;
      }

      let that = this;
      this.chart.setOption({
        color: ['#66B92E', '#FF3535'],
        legend: {
          show: false,
          left: 'center',
          bottom: '10',
          // data: ['Industries', 'Technology', 'Forex', 'Gold', 'Forecasts']
        },
        title: {
          show: true,
          zlevel: 0,
          text: ['{value|' + percent + '%' + '}', '{name|产出}'].join('\n'),
          rich: {
            value: {
              color: '#909399',
              fontSize: 20,
              fontWeight: 'bold',
              lineHeight: 33,
            },
            name: {
              color: '#909399',
              lineHeight: 20,
            },
          },
          top: 'center',
          left: 'center',
          // textAlign: "center",
          textStyle: {
            rich: {
              value: {
                color: '#909399',
                fontSize: 24,
                fontWeight: 'bold',
                lineHeight: 30,
              },
              name: {
                color: '#909399',
                lineHeight: 16,
              },
            },
          },
        },
        series: [
          {
            name: '任务',
            type: 'pie',
            radius: ['60%', '80%'],
            avoidLabelOverlap: false,

            label: {
              show: false,
              fontSize: 16,
              lineHeight: 20,
              position: 'center',
              // formatter: function (params) { // 默认显示第一个数据
              //   if (params.dataIndex === 0) {
              //     return '{p|' + params.percent + '%}' + '\n{nm|' + params.name + '}'
              //   } else {
              //     return ''
              //   }
              // },
              // emphasis: {
              //   formatter: function (params) {
              //     if (params.dataIndex != 0) {
              //       return '{p|' + params.percent + '%}' + '\n{nm|' + params.name + '}'
              //     }
              //   },
              // },
              // rich: {
              //   p: {
              //     fontSize: 26,
              //     // backgroundColor: "white" // 覆盖index=0时的数据
              //   },
              //   nm: {
              //     color: '#333',
              //     fontSize: 14,
              //     // backgroundColor: "white" // 覆盖index=0时的数据
              //   }
              // }
            },
            labelLine: {
              normal: {
                show: false, //引线隐藏
                length: 20,
                length2: 40,
              },
            },
            // emphasis: {
            //   label: {
            //     show: true,
            //     fontSize: '40',
            //     fontWeight: 'bold',
            //   }
            // },

            data: chartData,
          },
        ],
      });
    },
  },
};
</script>
